﻿using AutoMapper;
using BasicData.Application.Input;
using BasicData.Application.Input.BOM;
using BasicData.Application.Output;
using BasicData.Domain.Entities;
using BasicData.Infrastructure.Repositories;
using BasicData.Result;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace BasicData.Application.Services.BOM
{
    public class BOMServices : IBOMServices
    {
        IBasicModelRepository<BOMModel> repository;
        IBasicModelRepository<BomSparepart> repositorySparepart;
        IBasicModelRepository<ProductModel> repositoryMaterial;
       

        IMapper mapper;

        public BOMServices(IBasicModelRepository<BOMModel> repository, IMapper mapper, IBasicModelRepository<BomSparepart> repositorySparepart, IBasicModelRepository<ProductModel> repositoryMaterial)
        {
            this.repository = repository;
            this.mapper = mapper;
            this.repositorySparepart = repositorySparepart;
            this.repositoryMaterial = repositoryMaterial;
        }

        public ApiResult<int> AddBom(BomInput input)
        {
            var boms = mapper.Map<BOMModel>(input);
            boms.Id = YitIdHelper.NextId();
            boms.CreatedAt = DateTime.Now;
            boms.CreatedBy = "wyj";
            repository.Create(boms);
            return new ApiResult<int>()
            {
                Code = ApiEnums.OK,
                Message = "添加成功",
                Data = 1
            };
        }

       

        public ApiResult<int> DeleteBom(long id)
        {
            ApiResult<int> res = new ApiResult<int>();
            var model = repository.GetValue().FirstOrDefault(x => x.Id == id);
            if (model == null)
            {
                res.Code = ApiEnums.Fail;
                res.Message = "未找到该数据";
                return res;
            }
            model.IsDeleted = true;
            int i = repository.Update(model);
            if (i > 0)
            {
                res.Code = ApiEnums.OK;
                res.Message = "删除成功";
                res.Data = i;
            }
            else
            {
                res.Code = ApiEnums.Error;
                res.Message = "删除失败";
                res.Data = i;
            }
            return res;
        }

       

        public ApiPaging<BomOutput> GetBom(PageInput input)
        {
            ApiPaging<BomOutput> res = new ApiPaging<BomOutput>();
            var workshop = repository.GetValue().Where(x => x.IsDeleted == false);
            var query = workshop.OrderByDescending(x => x.Id).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
            var list = mapper.Map<List<BomOutput>>(query);
            return new ApiPaging<BomOutput>()
            {
                PageDatas = list,
                TotalCount = workshop.Count(),
                Code = ApiEnums.OK,
                Message = "获取成功",

            };
        }

      

        public ApiResult<int> UpdateBom(UpdateBomInput input)
        {
            var modedl = mapper.Map<BOMModel>(input);
            modedl.CreatedBy = "wyj";
            modedl.UpdatedAt = DateTime.Now;
            repository.Update(modedl);
            return new ApiResult<int>()
            {
                Code = ApiEnums.OK,
                Message = "修改成功",

            };
        }

        public ApiPaging<BomSparepartOutput> GetBomSparepart(PageInput input)
        {
            ApiPaging<BomSparepartOutput> res = new ApiPaging<BomSparepartOutput>();
            var bomSpareparts = repositorySparepart.GetValue().Where(x => x.IsDeleted == false);
            var products = repositoryMaterial.GetValue().Where(x => x.IsDeleted == false);
            var query = from a in bomSpareparts
                        join b in products on a.Product_id equals b.Id
                        select new BomSparepartOutput
                        {
                           Id = a.Id,
                           Product_id = b.Id,
                            ProductCode = b.ProductCode,
                            ProductName = b.ProductName,
                            Specification = b.Specification,
                            Unit = b.Unit,
                            Spend = a.Spend,
                             BomBfb = a.BomBfb,
                             NNumber = a.NNumber,
                             ParentId = a.ParentId,

                        };
            res.TotalCount = bomSpareparts.Count();
            res.PageDatas = query.OrderByDescending(x => x.Id).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
            res.Code = ApiEnums.OK;
            res.Message = "获取成功";
            return res;
        }
        public ApiResult<int> AddBomSparepart(BomSparepartInput input)
        {

            var boms = mapper.Map<BomSparepart>(input);
            boms.Id = YitIdHelper.NextId();
            boms.CreatedAt = DateTime.Now;
            boms.CreatedBy = "wyj";
            repositorySparepart.Create(boms);
            return new ApiResult<int>()
            {
                Code = ApiEnums.OK,
                Message = "添加成功",
                Data = 1
            };
        }
        public ApiResult<int> UpdateBomSparepart(UpdateBomSparepart input)
        {
            var modedl = mapper.Map<BomSparepart>(input);
            modedl.CreatedBy = "wyj";
            modedl.UpdatedAt = DateTime.Now;
            repositorySparepart.Update(modedl);
            return new ApiResult<int>()
            {
                Code = ApiEnums.OK,
                Message = "修改成功",

            };
        }
        public ApiResult<int> DeleteBomSparepart(long id)
        {
            ApiResult<int> res = new ApiResult<int>();
            var model = repositorySparepart.GetValue().FirstOrDefault(x => x.Id == id);
            if (model == null)
            {
                res.Code = ApiEnums.Fail;
                res.Message = "未找到该数据";
                return res;
            }
            model.IsDeleted = true;
            int i = repositorySparepart.Update(model);
            if (i > 0)
            {
                res.Code = ApiEnums.OK;
                res.Message = "删除成功";
                res.Data = i;
            }
            else
            {
                res.Code = ApiEnums.Error;
                res.Message = "删除失败";
                res.Data = i;
            }
            return res;
        }
    }
}
